package javagprssrv.Task;

import DT3.TrdUDPDataTrans;
import DT42.TrdTCPDataTrans_DT42;
import DT8.TrdTCPDataTrans_DT8;
import NBJL.TrdTCPDataTrans_NBJL;
import javagprssrv.DataAnalyse.TrdTCPDataTrans;
import javagprssrv.StructClass.structRecvData;
import javagprssrv.StructClass.structTerm;
import java.util.Date;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import javagprssrv.DataBase.DBSQLExecute;
import javagprssrv.Common.DateTime;
import javagprssrv.JGS_Param;
import javagprssrv.StructClass.structTask;
import java.util.Vector;

public class TrdTaskManager extends java.lang.Thread {

    int idxDBConnectionPool;
    int TaskMaxCount;
    java.util.Vector<javagprssrv.StructClass.structTask> tmpTasks;
    int TaskAutoGetDateMonth;

    public TrdTaskManager() {
        super("TrdTaskManager");
        this.idxDBConnectionPool = 0;
        this.TaskMaxCount = javagprssrv.JGS_Param._TaskMaxCount;
        this.tmpTasks = new java.util.Vector<javagprssrv.StructClass.structTask>();
        this.TaskAutoGetDateMonth = 8;
    }

    @java.lang.Override
    public void run() {
        this.TaskAutoGetDateMonth = javagprssrv.JGS_Param._TaskAutoGetDateMonth;
        try {
            java.lang.Thread.sleep(20000L);
        } catch (java.lang.Exception ex) {
        }
        while (java.lang.Boolean.TRUE) {
            try {
                --this.TaskAutoGetDateMonth;
                javagprssrv.JGS_Param.myLog.ShowLog(" - - - - - -    - - - - - -    - - - - - -    - - - - - -    - - - - - -    - - - - - -   TaskAutoGetDateMonth = " + this.TaskAutoGetDateMonth, java.lang.Boolean.TRUE, java.lang.Boolean.TRUE);
                if (this.TaskAutoGetDateMonth <= 0) {
                    this.TaskAutoGetDateMonth = javagprssrv.JGS_Param._TaskAutoGetDateMonth;
                }
                final java.lang.String startTime = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
                javagprssrv.JGS_Param.myLog.ShowLog("TrdTaskManager.Run().Start;", true, true);
                for (int i = javagprssrv.JGS_Param.TempTasks.size() - 1; i >= 0; --i) {
                    javagprssrv.JGS_Param.TempTasks.remove(i);
                }
                if (javagprssrv.JGS_Param.Tasks.size() >= javagprssrv.JGS_Param._TaskMaxCount) {
                    continue;
                }
                if (this.isGetDataTime()) {
                    final java.lang.String HHMM = new javagprssrv.Common.DateTime().getDateTime("YYYYMMDDHHNNSS").substring(8, 12);
                    this.Do_DT42_HuiGuan_T_Task();
                    this.Do_DT42_BuCai_T_Task();
                    this.Do_DT3_BuCai_T_Task();
                    if (this.TaskAutoGetDateMonth == 1) {
                        this.Do_DT42_BuCaiAllData();
                        this.Do_DT3_BuCaiAllData();
                    }
                    this.TmpTasks2Tasks();
                }
                javagprssrv.JGS_Param.myLog.ShowLog("TrdTaskManager.Run().Stop    [Start时间 : " + startTime + "];", true, true);
                java.lang.Thread.sleep(javagprssrv.JGS_Param._TaskManagerSeconds * 1000);
            } catch (java.lang.Exception e) {
            }
        }
    }

    public void Do_DT3_BuCaiAllData() {
        try {
            javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
            //final java.lang.String SQL = "select * from V_DeviceExtend where MODEL='DT3' or MODEL='DT3-GDIP'";
            //java.sql.ResultSet rs = dbse.SQL_Query(0, SQL);
            List<Map<String, Object>> list = dbse.findAllYbInfoByCond("where MODEL='DT3' or MODEL='DT3-GDIP'");
            for (Map<String, Object> m : list) {
                try {
                    String TelNO = (String) m.get("mobileNo");
                    String WYM = (String) m.get("uniqueCode");
                    String TermNo = (String) m.get("terminalNo");
                    java.lang.String cmd = "";
                    final java.util.Vector cmdS = new java.util.Vector();
                    final java.util.Calendar cal = java.util.Calendar.getInstance();
                    cal.add(5, -1);
                    final java.lang.String yesterday = new java.text.SimpleDateFormat("yyyyMMdd").format(cal.getTime());
                    cmd = "GET DATE " + yesterday.substring(4, 8);
                    cmdS.add(cmd);
                    cmd = "GET MONTH " + yesterday.substring(4, 6);
                    cmdS.add(cmd);
                    this.AddTmpTask("Scheme.SYSTEM.TaskManager", TelNO, TermNo, cmdS, true, WYM, "");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (java.lang.Exception ex) {
        }
    }

    public void Do_DT42_BuCaiAllData() {
        try {
            javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
            //final java.lang.String SQL = "select * from V_DeviceExtend where MODEL='DT4-2'";            
            List<Map<String, Object>> list = dbse.findAllYbInfoByCond("where MODEL='DT4-2'");
            for (Map<String, Object> m : list) {
                try {
                    final java.lang.String A1 = (String) m.get("adminAreaCode");
                    final java.lang.String A2 = (String) m.get("terminalNo");
                    final java.lang.String A3 = String.valueOf((Short) m.get("zbm"));
                    final java.lang.String Channel = (String) m.get("channel");
                    java.lang.String cmd = "";
                    final java.util.Vector cmdS = new java.util.Vector();
                    final java.util.Calendar cal = java.util.Calendar.getInstance();
                    cal.add(5, -1);
                    final java.lang.String yesterday = new java.text.SimpleDateFormat("yyyyMMdd").format(cal.getTime());
                    cmd = "GET DATE" + Channel + " " + yesterday.substring(4, 8);
                    cmdS.add(cmd);
                    cmd = "GET MONTH" + Channel + " " + yesterday.substring(4, 6);
                    cmdS.add(cmd);
                    this.Do_DT42_AddTmpTask(A1, A2, A3, Channel, cmdS);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }       
         
        } catch (java.lang.Exception ex) {
            ex.printStackTrace();
        }
    }

    public void Do_DT42_AddTmpTask(final java.lang.String A1, final java.lang.String A2, final java.lang.String A3, final java.lang.String Channel, final java.util.Vector cmdS) {
        this.AddTmpTask("Scheme.SYSTEM.TaskManager", A1 + A2, A2, cmdS, java.lang.Boolean.FALSE, A1 + "." + A2, "");
    }

    public void Do_DT42_BuCai_T_Task() {
        java.lang.String sql = "";
        final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        final java.util.Date dt = new java.util.Date();
        final java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sql = "select t.*,d.adminAreaCode+d.TerminalNO wym from T_TASK t left outer join t_device d on d.id=t.deviceID where t.status=0 and t.type<50 and d.ModelID in (select id from t_model where model ='DT4-2' ) order by wym";
        final java.sql.ResultSet rs = dbse.SQL_Query(this.idxDBConnectionPool, sql);
        java.lang.String TelNO = "";
        final java.util.Vector<java.lang.String> cmds = new java.util.Vector<java.lang.String>();
        final boolean CanBeAsyn = false;
        java.lang.String AsynTelNO = "";
        java.lang.String TermNo = "";
        java.lang.String CMD = "";
        final java.lang.String RunTime = "";
        java.lang.String User = "";
        java.lang.String DT42_DeviceID = "";
        java.lang.String WYM = "";
        java.lang.String Task_ID = "";
        try {
            while (rs.next()) {
                try {
                    Task_ID = rs.getString("id").trim();
                    User = "TASKManager_DT42(T_TASK)";
                    if (!rs.getString("wym").trim().equals(WYM)) {
                        if (!WYM.equals("")) {
                            this.AddTmpTask("Scheme." + User, WYM, TermNo, cmds, CanBeAsyn, AsynTelNO, RunTime);
                        }
                        cmds.clear();
                    }
                    final java.lang.String sql2 = "select * from V_DeviceExtend where ID=" + rs.getString("DeviceID").trim() + "";
                    final javagprssrv.DataBase.DBSQLExecute dbse2 = new javagprssrv.DataBase.DBSQLExecute();
                    final java.sql.ResultSet rs2 = dbse2.SQL_Query(this.idxDBConnectionPool, sql2);
                    rs2.next();
                    DT42_DeviceID = rs2.getString("ID").trim();
                    WYM = rs2.getString("AdminAreaCode").trim() + rs2.getString("TerminalNO").trim();
                    TelNO = rs2.getString("MobileNO").trim();
                    AsynTelNO = rs2.getString("AdminAreaCode").trim() + "." + rs2.getString("TerminalNO").trim();
                    TermNo = rs2.getString("TerminalNO").trim();
                    final int LS = rs2.getInt("Channel");
                    final java.lang.String TASK_RQ = rs.getString("DataTime").trim();
                    final int TASK_TYPE = rs.getInt("Type");
                    if (TASK_TYPE == 10) {
                        CMD = "GET DATE" + LS + " " + TASK_RQ.substring(4);
                    }
                    if (TASK_TYPE == 20) {
                        CMD = "GET MONTH" + LS + " " + TASK_RQ.substring(4);
                    }
                    cmds.add(CMD);
                } catch (java.lang.Exception ex) {
                }
                final java.lang.String statusSQL = "UPDATE T_Task set status=9 where ID='" + Task_ID + "'";
                dbse.SQL_Update(0, statusSQL);
            }
            if (!DT42_DeviceID.equals("")) {
                this.AddTmpTask("Scheme." + User, WYM, TermNo, cmds, CanBeAsyn, AsynTelNO, RunTime);
            }
        } catch (java.lang.Exception ex2) {
        }
    }

    public void Do_DT42_HuiGuan_T_Task() {
        java.lang.String sql = "";
        final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        final java.util.Date dt = new java.util.Date();
        final java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sql = "select t.*,d.adminAreaCode+d.TerminalNO wym from T_TASK t left outer join t_device d on d.id=t.deviceID where t.status<9 and t.Type>50 and d.ModelID in (select id from t_model where model ='DT4-2' ) order by wym";
        final java.sql.ResultSet rs = dbse.SQL_Query(this.idxDBConnectionPool, sql);
        java.lang.String TelNO = "";
        final java.util.Vector<java.lang.String> cmds = new java.util.Vector<java.lang.String>();
        final boolean CanBeAsyn = false;
        java.lang.String AsynTelNO = "";
        java.lang.String TermNo = "";
        java.lang.String CMD = "";
        final java.lang.String RunTime = "";
        java.lang.String User = "";
        java.lang.String DT42_DeviceID = "";
        java.lang.String WYM = "";
        java.lang.String Task_ID = "";
        try {
            while (rs.next()) {
                try {
                    Task_ID = rs.getString("id").trim();
                    User = "TASKManager_DT42(T_TASK)";
                    if (!rs.getString("wym").trim().equals(WYM)) {
                        if (!WYM.equals("")) {
                            this.AddTmpTask("Scheme." + User, WYM, TermNo, cmds, CanBeAsyn, AsynTelNO, RunTime);
                        }
                        cmds.clear();
                    }
                    final java.lang.String sql2 = "select VL.code,device.* from V_pointExtend device left outer join T_VoltageLevel VL on VL.id=device.VoltageLevelID  where device.DeviceID='" + rs.getString("DeviceID").trim() + "'";
                    final javagprssrv.DataBase.DBSQLExecute dbse2 = new javagprssrv.DataBase.DBSQLExecute();
                    final java.sql.ResultSet rs2 = dbse.SQL_Query(0, sql2);
                    rs2.next();
                    final int code = rs2.getInt("code");
                    int YB_BL = 1;
                    if (code == 2) {
                        YB_BL = 1;
                    }
                    if (code == 3) {
                        YB_BL = 1;
                    }
                    if (code == 7) {
                        YB_BL = 60;
                    }
                    if (code == 8) {
                        YB_BL = 100;
                    }
                    if (code == 9) {
                        YB_BL = 200;
                    }
                    if (code == 10) {
                        YB_BL = 350;
                    }
                    if (code == 12) {
                        YB_BL = 1100;
                    }
                    if (code == 13) {
                        YB_BL = 2200;
                    }
                    if (code == 14) {
                        YB_BL = 3300;
                    }
                    if (code == 15) {
                        YB_BL = 5000;
                    }
                    if (code == 16) {
                        YB_BL = 7500;
                    }
                    if (code == 17) {
                        YB_BL = 10000;
                    }
                    java.lang.System.out.println("code=" + code + ";YB_BL=" + YB_BL);
                    final java.lang.String sql3 = "select * from V_DeviceExtend where ID=" + rs.getString("DeviceID").trim() + "";
                    final javagprssrv.DataBase.DBSQLExecute dbse3 = new javagprssrv.DataBase.DBSQLExecute();
                    final java.sql.ResultSet rs3 = dbse3.SQL_Query(this.idxDBConnectionPool, sql3);
                    rs3.next();
                    DT42_DeviceID = rs3.getString("ID").trim();
                    WYM = rs3.getString("AdminAreaCode").trim() + rs3.getString("TerminalNO").trim();
                    TelNO = rs3.getString("MobileNO").trim();
                    AsynTelNO = rs3.getString("AdminAreaCode").trim() + "." + rs3.getString("TerminalNO").trim();
                    TermNo = rs3.getString("TerminalNO").trim();
                    final int LS = rs3.getInt("Channel");
                    final java.lang.String TASK_RQ = rs.getString("DataTime").trim();
                    final int TASK_TYPE = rs.getInt("Type");
                    if (TASK_TYPE == 100) {
                        CMD = "SET DATE" + LS + " " + TASK_RQ.substring(0);
                        final int _year = java.lang.Integer.valueOf(TASK_RQ.substring(0, 4));
                        final int _month = java.lang.Integer.valueOf(TASK_RQ.substring(4, 6));
                        final int _day = java.lang.Integer.valueOf(TASK_RQ.substring(6, 8));
                        final java.lang.String sql4 = "select * from T_RSJLock where DeviceID=" + DT42_DeviceID.trim() + " and dataYear=" + java.lang.String.valueOf(_year) + " and dataMonth=" + java.lang.String.valueOf(_month) + " and dataDay=" + java.lang.String.valueOf(_day);
                        final javagprssrv.DataBase.DBSQLExecute dbse4 = new javagprssrv.DataBase.DBSQLExecute();
                        final java.sql.ResultSet rs4 = dbse3.SQL_Query(this.idxDBConnectionPool, sql4);
                        rs4.next();
                        final int _zyxs = rs4.getInt("TotalRunMin");
                        final int _csxs = rs4.getInt("aboveUpperLimitMin");
                        final int _cxxs = rs4.getInt("underLowerLimitMin");
                        final int _csxc = (_csxs + 9) / 10;
                        final int _cxxc = (_cxxs + 9) / 10;
                        final int _gzs = 0;
                        final double _zdz = rs4.getDouble("maxVoltage") / YB_BL;
                        final double _zxz = rs4.getDouble("minVoltage") / YB_BL;
                        final double _pjz = rs4.getDouble("avgVoltage") / YB_BL;
                        java.lang.String _zdzs = rs4.getString("maxVoltageTime");
                        _zdzs = _zdzs.substring(0, 4) + _zdzs.substring(5, 7) + _zdzs.substring(8, 10) + _zdzs.substring(11, 13) + _zdzs.substring(14, 16) + _zdzs.substring(17, 19);
                        java.lang.String _zxzs = rs4.getString("minVoltageTime");
                        _zxzs = _zxzs.substring(0, 4) + _zxzs.substring(5, 7) + _zxzs.substring(8, 10) + _zxzs.substring(11, 13) + _zxzs.substring(14, 16) + _zxzs.substring(17, 19);
                        final java.lang.String CMD_data = " " + java.lang.String.valueOf(_zyxs) + " " + java.lang.String.valueOf(_csxs) + " " + java.lang.String.valueOf(_cxxs) + " " + java.lang.String.valueOf(_csxc) + " " + java.lang.String.valueOf(_cxxc) + " " + java.lang.String.valueOf(_gzs) + " " + java.lang.String.valueOf(_zdz) + " " + java.lang.String.valueOf(_zdzs) + " " + java.lang.String.valueOf(_zxz) + " " + java.lang.String.valueOf(_zxzs) + " " + java.lang.String.valueOf(_pjz);
                        CMD += CMD_data;
                    }
                    if (TASK_TYPE == 110) {
                        CMD = "SET DATE" + LS + " " + TASK_RQ.substring(0);
                        final int _year = java.lang.Integer.valueOf(TASK_RQ.substring(0, 4));
                        final int _month = java.lang.Integer.valueOf(TASK_RQ.substring(4, 6));
                        final int _day = java.lang.Integer.valueOf(TASK_RQ.substring(6, 8));
                        final java.lang.String sql4 = "select * from T_RSJ where DeviceID=" + DT42_DeviceID.trim() + " and dataYear=" + java.lang.String.valueOf(_year) + " and dataMonth=" + java.lang.String.valueOf(_month) + " and dataDay=" + java.lang.String.valueOf(_day);
                        final javagprssrv.DataBase.DBSQLExecute dbse4 = new javagprssrv.DataBase.DBSQLExecute();
                        final java.sql.ResultSet rs4 = dbse3.SQL_Query(this.idxDBConnectionPool, sql4);
                        rs4.next();
                        final int _zyxs = rs4.getInt("TotalRunMin");
                        final int _csxs = rs4.getInt("aboveUpperLimitMin");
                        final int _cxxs = rs4.getInt("underLowerLimitMin");
                        final int _csxc = (_csxs + 9) / 10;
                        final int _cxxc = (_cxxs + 9) / 10;
                        final int _gzs = 0;
                        final double _zdz = rs4.getDouble("maxVoltage") / YB_BL;
                        final double _zxz = rs4.getDouble("minVoltage") / YB_BL;
                        final double _pjz = rs4.getDouble("avgVoltage") / YB_BL;
                        java.lang.String _zdzs = rs4.getString("maxVoltageTime");
                        _zdzs = _zdzs.substring(0, 4) + _zdzs.substring(5, 7) + _zdzs.substring(8, 10) + _zdzs.substring(11, 13) + _zdzs.substring(14, 16) + _zdzs.substring(17, 19);
                        java.lang.String _zxzs = rs4.getString("minVoltageTime");
                        _zxzs = _zxzs.substring(0, 4) + _zxzs.substring(5, 7) + _zxzs.substring(8, 10) + _zxzs.substring(11, 13) + _zxzs.substring(14, 16) + _zxzs.substring(17, 19);
                        final java.lang.String CMD_data = " " + java.lang.String.valueOf(_zyxs) + " " + java.lang.String.valueOf(_csxs) + " " + java.lang.String.valueOf(_cxxs) + " " + java.lang.String.valueOf(_csxc) + " " + java.lang.String.valueOf(_cxxc) + " " + java.lang.String.valueOf(_gzs) + " " + java.lang.String.valueOf(_zdz) + " " + java.lang.String.valueOf(_zdzs) + " " + java.lang.String.valueOf(_zxz) + " " + java.lang.String.valueOf(_zxzs) + " " + java.lang.String.valueOf(_pjz);
                        CMD += CMD_data;
                    }
                    if (TASK_TYPE == 200) {
                        CMD = "SET MONTH" + LS + " " + TASK_RQ.substring(0);
                        final int _year = java.lang.Integer.valueOf(TASK_RQ.substring(0, 4));
                        final int _month = java.lang.Integer.valueOf(TASK_RQ.substring(4, 6));
                        final java.lang.String sql5 = "select * from T_YSJLock where DeviceID=" + DT42_DeviceID.trim() + " and dataYear=" + java.lang.String.valueOf(_year) + " and dataMonth=" + java.lang.String.valueOf(_month);
                        final javagprssrv.DataBase.DBSQLExecute dbse5 = new javagprssrv.DataBase.DBSQLExecute();
                        final java.sql.ResultSet rs5 = dbse3.SQL_Query(this.idxDBConnectionPool, sql5);
                        rs5.next();
                        final int _zyxs2 = rs5.getInt("TotalRunMin");
                        final int _csxs2 = rs5.getInt("aboveUpperLimitMin");
                        final int _cxxs2 = rs5.getInt("underLowerLimitMin");
                        final int _csxc2 = (_csxs2 + 9) / 10;
                        final int _cxxc2 = (_cxxs2 + 9) / 10;
                        final int _gzs2 = 0;
                        final double _zdz2 = rs5.getDouble("maxVoltage") / YB_BL;
                        final double _zxz2 = rs5.getDouble("minVoltage") / YB_BL;
                        final double _pjz2 = rs5.getDouble("avgVoltage") / YB_BL;
                        java.lang.String _zdzs2 = rs5.getString("maxVoltageTime");
                        _zdzs2 = _zdzs2.substring(0, 4) + _zdzs2.substring(5, 7) + _zdzs2.substring(8, 10) + _zdzs2.substring(11, 13) + _zdzs2.substring(14, 16) + _zdzs2.substring(17, 19);
                        java.lang.String _zxzs2 = rs5.getString("minVoltageTime");
                        _zxzs2 = _zxzs2.substring(0, 4) + _zxzs2.substring(5, 7) + _zxzs2.substring(8, 10) + _zxzs2.substring(11, 13) + _zxzs2.substring(14, 16) + _zxzs2.substring(17, 19);
                        final java.lang.String CMD_data2 = " " + java.lang.String.valueOf(_zyxs2) + " " + java.lang.String.valueOf(_csxs2) + " " + java.lang.String.valueOf(_cxxs2) + " " + java.lang.String.valueOf(_csxc2) + " " + java.lang.String.valueOf(_cxxc2) + " " + java.lang.String.valueOf(_gzs2) + " " + java.lang.String.valueOf(_zdz2) + " " + java.lang.String.valueOf(_zdzs2) + " " + java.lang.String.valueOf(_zxz2) + " " + java.lang.String.valueOf(_zxzs2) + " " + java.lang.String.valueOf(_pjz2);
                        CMD += CMD_data2;
                    }
                    if (TASK_TYPE == 210) {
                        CMD = "SET MONTH" + LS + " " + TASK_RQ.substring(0);
                        final int _year = java.lang.Integer.valueOf(TASK_RQ.substring(0, 4));
                        final int _month = java.lang.Integer.valueOf(TASK_RQ.substring(4, 6));
                        final java.lang.String sql5 = "select * from T_YSJ where DeviceID=" + DT42_DeviceID.trim() + " and dataYear=" + java.lang.String.valueOf(_year) + " and dataMonth=" + java.lang.String.valueOf(_month);
                        final javagprssrv.DataBase.DBSQLExecute dbse5 = new javagprssrv.DataBase.DBSQLExecute();
                        final java.sql.ResultSet rs5 = dbse3.SQL_Query(this.idxDBConnectionPool, sql5);
                        rs5.next();
                        final int _zyxs2 = rs5.getInt("TotalRunMin");
                        final int _csxs2 = rs5.getInt("aboveUpperLimitMin");
                        final int _cxxs2 = rs5.getInt("underLowerLimitMin");
                        final int _csxc2 = (_csxs2 + 9) / 10;
                        final int _cxxc2 = (_cxxs2 + 9) / 10;
                        final int _gzs2 = 0;
                        final double _zdz2 = rs5.getDouble("maxVoltage") / YB_BL;
                        final double _zxz2 = rs5.getDouble("minVoltage") / YB_BL;
                        final double _pjz2 = rs5.getDouble("avgVoltage") / YB_BL;
                        java.lang.String _zdzs2 = rs5.getString("maxVoltageTime");
                        _zdzs2 = _zdzs2.substring(0, 4) + _zdzs2.substring(5, 7) + _zdzs2.substring(8, 10) + _zdzs2.substring(11, 13) + _zdzs2.substring(14, 16) + _zdzs2.substring(17, 19);
                        java.lang.String _zxzs2 = rs5.getString("minVoltageTime");
                        _zxzs2 = _zxzs2.substring(0, 4) + _zxzs2.substring(5, 7) + _zxzs2.substring(8, 10) + _zxzs2.substring(11, 13) + _zxzs2.substring(14, 16) + _zxzs2.substring(17, 19);
                        final java.lang.String CMD_data2 = " " + java.lang.String.valueOf(_zyxs2) + " " + java.lang.String.valueOf(_csxs2) + " " + java.lang.String.valueOf(_cxxs2) + " " + java.lang.String.valueOf(_csxc2) + " " + java.lang.String.valueOf(_cxxc2) + " " + java.lang.String.valueOf(_gzs2) + " " + java.lang.String.valueOf(_zdz2) + " " + java.lang.String.valueOf(_zdzs2) + " " + java.lang.String.valueOf(_zxz2) + " " + java.lang.String.valueOf(_zxzs2) + " " + java.lang.String.valueOf(_pjz2);
                        CMD += CMD_data2;
                    }
                    cmds.add(CMD);
                } catch (java.lang.Exception ex) {
                }
                final java.lang.String statusSQL = "UPDATE T_Task set status=status+1 where ID='" + Task_ID + "'";
                dbse.SQL_Update(0, statusSQL);
            }
            if (!DT42_DeviceID.equals("")) {
                this.AddTmpTask("Scheme." + User, WYM, TermNo, cmds, CanBeAsyn, AsynTelNO, RunTime);
            }
        } catch (java.lang.Exception ex2) {
        }
    }

    public void Do_DT3_BuCai_T_Task() {
        java.lang.String sql = "";
        final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        final java.util.Date dt = new java.util.Date();
        final java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sql = "select t.*,d.* from T_TASK t left outer join t_device d on d.id=t.deviceID where t.status=0 and d.ModelID in (select id from t_model where model ='DT3' or model ='DT3-GDIP') order by t.DeviceID";
        final java.sql.ResultSet rs = dbse.SQL_Query(this.idxDBConnectionPool, sql);
        java.lang.String TelNO = "";
        final java.util.Vector<java.lang.String> cmds = new java.util.Vector<java.lang.String>();
        final boolean CanBeAsyn = false;
        java.lang.String AsynTelNO = "";
        java.lang.String TermNo = "";
        java.lang.String CMD = "";
        final java.lang.String RunTime = "";
        java.lang.String User = "";
        final java.lang.String DT42_DeviceID = "";
        final java.lang.String WYM = "";
        java.lang.String Task_ID = "";
        java.lang.String DeviceID = "";
        try {
            while (rs.next()) {
                try {
                    Task_ID = rs.getString("id").trim();
                    User = "TASKManager_DT3(T_TASK)";
                    if (!rs.getString("DeviceID").trim().equals(DeviceID)) {
                        if (!DeviceID.equals("")) {
                            this.AddTmpTask("Scheme." + User, TelNO, TermNo, cmds, CanBeAsyn, AsynTelNO, RunTime);
                        }
                        cmds.clear();
                    }
                    TelNO = rs.getString("MobileNO").trim();
                    TermNo = rs.getString("TerminalNo").trim();
                    AsynTelNO = rs.getString("uniqueCode").trim();
                    DeviceID = rs.getString("DeviceID").trim();
                    final java.lang.String TASK_RQ = rs.getString("DataTime").trim();
                    final int TASK_TYPE = rs.getInt("Type");
                    if (TASK_TYPE == 10) {
                        CMD = "GET DATE " + TASK_RQ.substring(4);
                    }
                    if (TASK_TYPE == 20) {
                        CMD = "GET MONTH " + TASK_RQ.substring(4);
                    }
                    cmds.add(CMD);
                } catch (java.lang.Exception ex) {
                }
                final java.lang.String statusSQL = "UPDATE T_Task set status=9 where ID='" + Task_ID + "'";
                dbse.SQL_Update(0, statusSQL);
            }
            if (!DeviceID.equals("")) {
                this.AddTmpTask("Scheme." + User, TelNO, TermNo, cmds, CanBeAsyn, AsynTelNO, RunTime);
            }
        } catch (java.lang.Exception ex2) {
        }
    }

    public void DeleteOldGetDataLog() {
        java.lang.String sql = "";
        javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        final java.util.Date dt = new java.util.Date();
        java.util.Date dt2 = new java.util.Date(dt.getTime() - javagprssrv.JGS_Param._GetDataLogDeleteSeconds * 1000);
        final java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sql = "delete from t_getDataLog where isnull(结果,0)<>-1 and 开始时间<='" + sdf.format(dt2) + "'";
        dt2 = new java.util.Date(dt.getTime() - javagprssrv.JGS_Param._AfterErrorRetrySeconds * 1000);
        sql = "delete from t_getDataLog where isnull(结果,0)=-1 and 开始时间<='" + sdf.format(dt2) + "'";
        dt2 = new java.util.Date(new java.util.Date().getTime() - 2592000000L);
        sql = "delete from t_getDataScheme where 创建时间<='" + sdf.format(dt2) + "'";
        dt2 = new java.util.Date(dt.getTime() - 2592000000L);
        final java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
        sql = "delete from T_ErrorLog where 执行日期<='" + sdf2.format(dt2).substring(0, 8) + "'";
        dbse = null;
    }

    //结构不符
    public void DoGetDataScheme() {
        java.lang.String sql = "";
        final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        final java.util.Date dt = new java.util.Date();
        final java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sql = "select * from T_GetDataScheme where 执行时间<='" + sdf.format(dt) + "' and 状态=0 order by 手机卡号";
        final java.sql.ResultSet rs = dbse.SQL_Query(this.idxDBConnectionPool, sql);
        java.lang.String TelNO = "";
        final java.util.Vector<java.lang.String> cmds = new java.util.Vector<java.lang.String>();
        boolean CanBeAsyn = false;
        java.lang.String AsynTelNO = "";
        java.lang.String TermNo = "";
        java.lang.String CMD = "";
        java.lang.String RunTime = "";
        java.lang.String User = "";
        try {
            while (rs.next()) {
                try {
                    User = rs.getString("用户ID").trim();
                    if (!rs.getString("手机卡号").trim().equals(TelNO)) {
                        if (!TelNO.equals("")) {
                            this.AddTmpTask("Scheme." + User, TelNO, TermNo, cmds, CanBeAsyn, AsynTelNO, RunTime);
                        }
                        cmds.clear();
                    }
                    final java.lang.String sql2 = "select * from V_YB_CanASYN where 手机卡号='" + rs.getString("手机卡号").trim() + "'";
                    final javagprssrv.DataBase.DBSQLExecute dbse2 = new javagprssrv.DataBase.DBSQLExecute();
                    final java.sql.ResultSet rs2 = dbse2.SQL_Query(this.idxDBConnectionPool, sql2);
                    rs2.next();
                    TelNO = rs2.getString("手机卡号").trim();
                    AsynTelNO = rs2.getString("唯一码").trim();
                    TermNo = rs2.getString("终端号").trim();
                    CanBeAsyn = (rs2.getInt("重复") == 1);
                    CMD = rs.getString("命令").trim();
                    cmds.add(CMD);
                    RunTime = rs.getString("执行时间").trim();
                    final java.lang.String date = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
                    final java.lang.String sql3 = "update T_GetDataScheme set 状态=1,执行时间='" + date + "' where 状态=0 and 手机卡号='" + TelNO + "' and 命令='" + CMD + "' and 执行时间='" + RunTime + "'";
                    dbse2.SQL_Update(this.idxDBConnectionPool, sql3);
                } catch (java.lang.Exception e2) {
                }
            }
            if (!TelNO.equals("")) {
                this.AddTmpTask("Scheme." + User, TelNO, TermNo, cmds, CanBeAsyn, AsynTelNO, RunTime);
            }
        } catch (java.lang.Exception ex) {
        }
    }

    public boolean isGetDataTime() {
        boolean canGetData = true;
        final java.util.Date dt = new java.util.Date();
        final int h = dt.getHours();
        if (h == 0) {
            canGetData = false;
        }
        return canGetData;
    }

    //结构不符
    public void DoGetUnGetDate() {
        final java.util.Date dt = new java.util.Date();
        final java.util.Date dt2 = new java.util.Date(dt.getTime() - javagprssrv.JGS_Param._GetDataBeforeDays * 24 * 60 * 60 * 1000);
        final java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");
        final java.lang.String sql = "select * from V_unGetDate where 日期>='" + sdf.format(dt2) + "' order by 手机卡号,日期";
        final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        final java.sql.ResultSet rs = dbse.SQL_Query(this.idxDBConnectionPool, sql);
        java.lang.String TelNO = "";
        final java.util.Vector<java.lang.String> cmds = new java.util.Vector<java.lang.String>();
        boolean CanBeAsyn = false;
        java.lang.String AsynTelNO = "";
        java.lang.String TermNo = "";
        final java.lang.String CMD = "";
        final java.lang.String RunTime = "";
        try {
            while (rs.next()) {
                if (!rs.getString("手机卡号").trim().equals(TelNO)) {
                    if (!TelNO.equals("")) {
                        this.AddTmpTask("System.TaskManager", TelNO, TermNo, cmds, CanBeAsyn, AsynTelNO, RunTime);
                    }
                    cmds.clear();
                }
                TelNO = rs.getString("手机卡号").trim();
                AsynTelNO = rs.getString("唯一码").trim();
                TermNo = rs.getString("终端号").trim();
                CanBeAsyn = (rs.getInt("重复") == 1);
                final java.lang.String date = rs.getString("日期").trim().substring(4, 8);
                cmds.add("get date " + date);
            }
            if (!TelNO.equals("")) {
                this.AddTmpTask("System.TaskManager", TelNO, TermNo, cmds, CanBeAsyn, AsynTelNO, RunTime);
            }
        } catch (java.lang.Exception ex) {
        }
    }

    //结构不符
    public void DoGetUnGetMonth() {
        final java.util.Date dt = new java.util.Date();
        final java.util.Date dt2 = new java.util.Date(dt.getTime() - javagprssrv.JGS_Param._GetDataBeforeDays * 24 * 60 * 60 * 1000);
        final java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");
        final java.lang.String sql = "select * from V_unGetMonth6  order by 手机卡号,年月";
        final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        final java.sql.ResultSet rs = dbse.SQL_Query(this.idxDBConnectionPool, sql);
        java.lang.String TelNO = "";
        final java.util.Vector<java.lang.String> cmds = new java.util.Vector<java.lang.String>();
        boolean CanBeAsyn = false;
        java.lang.String AsynTelNO = "";
        java.lang.String TermNo = "";
        final java.lang.String CMD = "";
        final java.lang.String RunTime = "";
        try {
            while (rs.next()) {
                if (!rs.getString("手机卡号").trim().equals(TelNO)) {
                    if (!TelNO.equals("")) {
                        this.AddTmpTask("System.TaskManager", TelNO, TermNo, cmds, CanBeAsyn, AsynTelNO, RunTime);
                    }
                    cmds.clear();
                }
                TelNO = rs.getString("手机卡号").trim();
                AsynTelNO = rs.getString("唯一码").trim();
                TermNo = rs.getString("终端号").trim();
                CanBeAsyn = (rs.getInt("重复") == 1);
                final java.lang.String yue = rs.getString("年月").trim().substring(4, 6);
                cmds.add("get month " + yue);
            }
            if (!TelNO.equals("")) {
                this.AddTmpTask("System.TaskManager", TelNO, TermNo, cmds, CanBeAsyn, AsynTelNO, RunTime);
            }
        } catch (java.lang.Exception ex) {
        }finally{
            if(rs!=null){
                try{
                    rs.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    public static synchronized void _AddTask(java.lang.String Creator, final java.lang.String TermTelNO, final java.lang.String TermNO, final java.util.Vector<java.lang.String> CommandS, final boolean CanBeAsyn, final java.lang.String TaskAsynTelNO, final java.lang.String SchemeDateTime) {
        final javagprssrv.StructClass.structTask Task = new javagprssrv.StructClass.structTask();
        boolean isTaskRight = true;
        if (TermTelNO.trim().length() != 11) {
            isTaskRight = false;
        }
        if (TermNO.trim().length() == 0) {
            isTaskRight = false;
        }
        if (!isTaskRight) {
        }
        int TermType = 0;
        Task.isTCP = false;
        java.lang.String IP = "";
        int Port = -1;
        boolean isFindTCPTerm = false;
        final java.lang.String sql_DT42 = "select * from V_DeviceExtend where AdminAreaCode+TerminalNo='" + TermTelNO + "'";
        final javagprssrv.DataBase.DBSQLExecute dbse_DT42 = new javagprssrv.DataBase.DBSQLExecute();
        final java.sql.ResultSet rs_DT42 = dbse_DT42.SQL_Query(0, sql_DT42);
        try {
            rs_DT42.next();
            final java.lang.String wym = rs_DT42.getString("AdminAreaCode").trim() + "." + rs_DT42.getString("TerminalNO").trim();
            Task.isTCP = true;
            Task.TaskAsynTelNO = wym.trim();
            Task.TermType = 42;
            Task.Task_State = 1;
            TermType = 42;
            if (wym.length() <= 0) {
                return;
            }
            int idx = -1;
            for (int i = javagprssrv.JGS_Param.TCP_Terms.size() - 1; i >= 0; --i) {
                if (javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_WYM == null) {
                    javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_WYM = "";
                }
                if (javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_Type == null) {
                    javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_Type = "";
                }
                if (javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_Type.equals("42")) {
                    final java.lang.String _wym = javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_WYM;
                    if (_wym.equals(wym)) {
                        idx = i;
                        break;
                    }
                }
            }
            if (idx <= -1) {
                isFindTCPTerm = false;
                return;
            }
            IP = javagprssrv.JGS_Param.TCP_Terms.elementAt(idx).Term_RemoteIP;
            Port = javagprssrv.JGS_Param.TCP_Terms.elementAt(idx).Term_RemotePort;
            Task.TaskAsynTelNO_ALL = javagprssrv.JGS_Param.TCP_Terms.elementAt(idx).Term_WYM;
            Task.BindGPRS_IP = IP;
            Task.BindGPRS_Port = java.lang.String.valueOf(Port);
            isFindTCPTerm = true;
        } catch (java.lang.Exception ex) {
        }
        final java.lang.String sql_DT3GDIP = "select * from V_DeviceExtend where model='DT3-GDIP' and  MobileNo='" + TermTelNO + "'";
        final javagprssrv.DataBase.DBSQLExecute dbse_DT3GDIP = new javagprssrv.DataBase.DBSQLExecute();
        final java.sql.ResultSet rs_DT3GDIP = dbse_DT3GDIP.SQL_Query(0, sql_DT3GDIP);
        try {
            rs_DT3GDIP.next();
            final java.lang.String wym2 = rs_DT3GDIP.getString("uniqueCode").trim();
            Task.isTCP = false;
            Task.TaskAsynTelNO = wym2.trim();
            Task.BindGPRS_IP = rs_DT3GDIP.getString("ip").trim();
            Task.BindGPRS_Port = rs_DT3GDIP.getString("port").trim();
            Task.TermType = 1;
            Task.Task_State = 1;
            TermType = 1;
        } catch (java.lang.Exception ex2) {
        }
        Task.Task_Creater = Creator;
        Task.Task_TelNo = TermTelNO;
        Task.Task_TermNo = TermNO;
        Task.TermType = TermType;
        Task.Task_Jobs = CommandS;
        Task.Task_OriginJobs = CommandS;
        Task.TaskCanBeAsyn = CanBeAsyn;
        Task.TaskAsynTelNO = TaskAsynTelNO;
        Task.Task_SchemeDateTime = SchemeDateTime;
        Task.Task_JobsIndex = -1;
        final java.lang.String TaskID = Task._TaskID;
        Creator = Creator.trim() + "           ";
        if (Creator.toUpperCase().substring(0, 7).equals("IADATA@")) {
            java.lang.String ip = "";
            int port = -1;
            java.lang.String s = Creator.trim();
            s = s.substring(s.indexOf("@") + 1);
            ip = s.substring(0, s.indexOf(":"));
            port = java.lang.Integer.valueOf(s.substring(s.indexOf(":") + 1));
            Task.Task_isJSSJ = true;
            Task.JSSJ_ClientIP = ip;
            Task.JSSJ_ClientPort = port;
            javagprssrv.JGS_Param.Tasks.add(0, Task);
        } else {
            javagprssrv.JGS_Param.Tasks.add(Task);
        }
        if (Task.TermType == 2) {
            final javagprssrv.StructClass.structRecvData recvData = new javagprssrv.StructClass.structRecvData();
            recvData.Protocol = "TCP";
            recvData.RemoteIP = Task.BindGPRS_IP;
            recvData.RemotePort = java.lang.Integer.valueOf(Task.BindGPRS_Port);
            recvData.Data = "Y" + Task.TaskAsynTelNO + "Z";
            new javagprssrv.DataAnalyse.TrdTCPDataTrans(recvData).start();
        }
        if (Task.TermType == 6) {
            final javagprssrv.StructClass.structRecvData recvData = new javagprssrv.StructClass.structRecvData();
            recvData.Protocol = "TCP";
            recvData.RemoteIP = Task.BindGPRS_IP;
            recvData.RemotePort = java.lang.Integer.valueOf(Task.BindGPRS_Port);
            recvData.Data = "Y" + Task.TaskAsynTelNO + "Z";
            new NBJL.TrdTCPDataTrans_NBJL(recvData).start();
        }
        if (Task.TermType == 8) {
            final javagprssrv.StructClass.structRecvData recvData = new javagprssrv.StructClass.structRecvData();
            recvData.Protocol = "TCP";
            recvData.RemoteIP = Task.BindGPRS_IP;
            recvData.RemotePort = java.lang.Integer.valueOf(Task.BindGPRS_Port);
            recvData.Data = "Y" + Task.TaskAsynTelNO + "Z";
            new DT8.TrdTCPDataTrans_DT8(recvData).start();
        }
        if (Task.TermType == 42) {
            final javagprssrv.StructClass.structRecvData recvData = new javagprssrv.StructClass.structRecvData();
            recvData.Protocol = "TCP";
            recvData.RemoteIP = Task.BindGPRS_IP;
            recvData.RemotePort = java.lang.Integer.valueOf(Task.BindGPRS_Port);
            recvData.Data = "Y" + Task.TaskAsynTelNO + "Z";
            new DT42.TrdTCPDataTrans_DT42(recvData).start();
        }
        if (Task.TermType == 1) {
            final javagprssrv.StructClass.structRecvData recvData = new javagprssrv.StructClass.structRecvData();
            recvData.Protocol = "UDP";
            recvData.RemoteIP = Task.BindGPRS_IP;
            recvData.RemotePort = java.lang.Integer.valueOf(Task.BindGPRS_Port);
            recvData.Data = "Y" + Task.TaskAsynTelNO + "Z";
            new DT3.TrdUDPDataTrans(recvData).start();
        }
        javagprssrv.JGS_Param.myLog.ShowLog("[新任务]ID=" + TaskID + ",Creator=" + Creator.trim() + ",TelNO=" + TermTelNO + ",TermNO=" + TermNO + ",CommandS=" + CommandS.toString() + ",CanBeAsyn=" + java.lang.String.valueOf(CanBeAsyn) + ",AsynTelNO=" + TaskAsynTelNO, true, true);
    }

    private synchronized void AddTmpTask(final java.lang.String Creator, final java.lang.String TermTelNO, final java.lang.String TermNO, final java.util.Vector<java.lang.String> CommandS, final boolean CanBeAsyn, final java.lang.String TaskAsynTelNO, final java.lang.String SchemeDateTime) {
        final javagprssrv.StructClass.structTask Task = new javagprssrv.StructClass.structTask();
        boolean isTaskRight = true;
        if (TermTelNO.trim().length() != 11) {
            isTaskRight = false;
        }
        if (TermNO.trim().length() == 0) {
            isTaskRight = false;
        }
        if (!isTaskRight) {
        }
        int idxTmpTasks = -1;
        for (int i = 0; i < javagprssrv.JGS_Param.TempTasks.size(); ++i) {
            if (javagprssrv.JGS_Param.TempTasks.elementAt(i).Task_TelNo.equals(TermTelNO)) {
                idxTmpTasks = i;
                break;
            }
        }
        if (idxTmpTasks == -1) {
            javagprssrv.JGS_Param.TempTasks.add(Task);
            idxTmpTasks = javagprssrv.JGS_Param.TempTasks.size() - 1;
        }
        final int TermType = 0;
        javagprssrv.JGS_Param.TempTasks.elementAt(idxTmpTasks).Task_Creater = Creator;
        javagprssrv.JGS_Param.TempTasks.elementAt(idxTmpTasks).Task_TelNo = TermTelNO;
        javagprssrv.JGS_Param.TempTasks.elementAt(idxTmpTasks).Task_TermNo = TermNO;
        javagprssrv.JGS_Param.TempTasks.elementAt(idxTmpTasks).TermType = TermType;
        javagprssrv.JGS_Param.TempTasks.elementAt(idxTmpTasks).TaskCanBeAsyn = CanBeAsyn;
        javagprssrv.JGS_Param.TempTasks.elementAt(idxTmpTasks).TaskAsynTelNO = TaskAsynTelNO;
        javagprssrv.JGS_Param.TempTasks.elementAt(idxTmpTasks).Task_SchemeDateTime = SchemeDateTime;
        javagprssrv.JGS_Param.TempTasks.elementAt(idxTmpTasks).Task_JobsIndex = -1;
        for (int j = 0; j < CommandS.size(); ++j) {
            javagprssrv.JGS_Param.TempTasks.elementAt(idxTmpTasks).Task_Jobs.add(CommandS.elementAt(j));
        }
        final java.lang.String creator = Creator + "         ";
        if (creator.toUpperCase().substring(0, 7).equals("SCHEME.")) {
            javagprssrv.JGS_Param.TempTasks.elementAt(idxTmpTasks).isScheme = true;
        } else {
            javagprssrv.JGS_Param.TempTasks.elementAt(idxTmpTasks).isScheme = false;
        }
    }

    private synchronized void TmpTasks2Tasks() {
        int counts = 0;
        for (int i = 0; i < javagprssrv.JGS_Param.TempTasks.size() && javagprssrv.JGS_Param.Tasks.size() < javagprssrv.JGS_Param._TaskMaxCount; ++i) {
            _AddTask(javagprssrv.JGS_Param.TempTasks.elementAt(i).Task_Creater, javagprssrv.JGS_Param.TempTasks.elementAt(i).Task_TelNo, javagprssrv.JGS_Param.TempTasks.elementAt(i).Task_TermNo, javagprssrv.JGS_Param.TempTasks.elementAt(i).Task_Jobs, javagprssrv.JGS_Param.TempTasks.elementAt(i).TaskCanBeAsyn, javagprssrv.JGS_Param.TempTasks.elementAt(i).TaskAsynTelNO, javagprssrv.JGS_Param.TempTasks.elementAt(i).Task_SchemeDateTime);
            counts = i + 1;
        }
    }
}
